package com.xtayfjpk.security;

import java.security.KeyStore;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.X509KeyManager;

import org.junit.Test;

import com.xtayfjpk.security.utils.SecurityUtils;

/**
 * 一个KeyManager用于管理KeyStore中的所有PrivateKeyEntry
 * @author zj
 *
 */
public class KeyManagerTest {
	
	@Test
	public void test() throws Exception {
		KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		String password = "gitblit";
		String keyStorePassword = password;
		String keyStorePath = "D:\\installations\\gitblit-1.6.0\\data\\serverKeyStore.jks";
		KeyStore ks = SecurityUtils.getKeyStore(keyStorePath, "jks", keyStorePassword);
		keyManagerFactory.init(ks, password.toCharArray());
		KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
		System.out.println(keyManagers.length);
		X509KeyManager keyManager = (X509KeyManager) keyManagers[0];
		System.out.println(keyManager.getClass());
		System.out.println(keyManager.getPrivateKey("localhost"));
		System.out.println(keyManager.getPrivateKey("192.168.0.181"));
	}
}
